<template>
  <div class="cart">
    <!-- 空页面 -->
    <div class="dalogon" v-if="!$store.getters.getCartGoods.length">
      <div class="img">
        <div class="big">
          <div class="smll">
            <img src="@/assets/img/cart/emptyCart.png" alt="">
          </div>
        </div>
      </div>
      <div class="text">
        <p>您的购物车是空的</p>
      </div>
      <div class="quxiangou">
        <router-link to="login" v-if="!$store.getters.getUserInfo">
          <p>去登录</p>
        </router-link>
        <router-link to="/" v-else>
          <p>去选购商品</p>
        </router-link>

      </div>
    </div>
    <!-- 空页面 End-->
    <!-- 购物车数据 -->
    <div class="goods" v-else>

      <div class="items">

        <div class="item" v-for="item, index in this.$store.getters.getCartGoods" :key="index">

          <input class="ched" type="checkbox" @change="updateCart(item)" v-model="item.sel">
          <div class="img"><img :src="item.img_url" alt=""></div>
          <div class="text">
            <div class="name">{{ item.name }}</div>
            <div class="dele" @click="removeCart(item.id)">删除</div>
            <div class="butom">
              <div class="jia">
                <div class="price">优惠价￥{{ item.price }}</div>
              </div>
              <div class="num">

                <van-stepper @change="updateCart(item)" v-model="item.num" min="1" :max="item.multi_buy_limit.first" />
              </div>
             
            </div>


          </div>
        </div>
      </div>


      <!-- 全选 -->
      <van-submit-bar :price="total()" button-text="提交订单">
        <van-checkbox v-model="checkedAll" @click="changeAll">全选</van-checkbox>

      </van-submit-bar>
      <!-- 全选 End-->

    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      checkedAll: true,
    }
  },
  created() {
    this.$store.dispatch("getCaryAsync").then(res => {

      console.log(res, 'chushua');
      //确定是否是全选checkedAll
      this.setcheckedAll()
    })
  },
  methods: {
    updateCart(good) {
      this.$store.dispatch("updateCartAsync", good).then(res => {
        if (res.code == 0) {
          this.$toast("修改成功")

        }
        this.setcheckedAll()
      })
    },

    total() {

      let all = 0;
      this.$store.getters.getCartGoods.forEach(item => {
        if (item.sel == 1) {
          all += item.price * item.num
        }
      })

      return all * 100
    },
    setcheckedAll() {

      this.checkedAll = this.$store.getters.getCartGoods.every(item => {
        return item.sel == 1
      })
    },
    changeAll() {

      let all = true
      all = this.$store.getters.getCartGoods.every(item => {
        return item.sel == 1
      })
      if (all) {
        this.checkAll = false
        this.$store.getters.getCartGoods.forEach(item => {
          item.sel = 0
          this.updateCart(item)
        })
      }
      else {
        this.checkAll = true
        this.$store.getters.getCartGoods.forEach(item => {
          item.sel = 1
          this.updateCart(item)
        })
      }
    },
    removeCart(id){
      console.log(id);
      this.$store.dispatch("deleteCartAsync", id).then(res => {
        if (res.code == 0) {
          this.$toast("删除成功")

        }
        this.setcheckedAll()
      })
    }
  },
}
</script>
<style lang="less" scoped>
.cart {
  .dalogon {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    width: 100vw;
    height: calc(100vh - 54px);
    background-color: #fff;


    .img {
      .big {
        width: 120px;
        height: 120px;
        display: flex;
        justify-content: center;
        align-items: center;
        background: #ffe4e6;
        border-radius: 50%;
        margin-bottom: 10px;

        .smll {
          width: 100px;
          height: 100px;
          background: #ffccd1;
          border-radius: 50%;

          img {
            width: 80px;
            height: 80px;
          }
        }

      }

    }

    .text {
      color: #b6b6b7;
      font-size: 12px;
    }

    .quxiangou {
      width: 140px;
      height: 38px;
      line-height: 38px;
      margin: 10px;
      border: 1px solid #f35b6b;
      color: #f35b6b;
      border-radius: 10px;
    }


  }

  //空页面end
  .goods {
    padding-top: 10px;
    height: 100vh;
   background-color:  #f4f4f4;
    .items {
      width: 95%;
      background: #fff;
      margin: 0 auto;
      border-radius: 10px;
      display: flex;
      flex-direction: column;
      justify-content: flex-start;
      align-items: center;

      .item {
        width: 100%;
        display: flex;
        justify-content: space-evenly;
        align-items: center;
        height: 130px;
       
        margin: 10px;
        padding: 10px;

        .img {
          width: 85px;
          height: 85px;
        }

        .text {
          height: 100%;
          display: flex;
          flex-direction: column;
          margin: 10px;
          padding: 10px;
          justify-content: space-between;
          width: 80%;
          .dele{
            text-align: right;
          }
          .name {
            font-size: 14px;
            text-align: left;
            width: 100%;
            height: 29px;
            display: -webkit-box;
            overflow: hidden;
            text-overflow: ellipsis;
            -webkit-line-clamp: 2;
            -webkit-box-orient: vertical;
          }

          .butom{
            font-size: 16px;
            width: 100%;
            display: flex;
            justify-content: space-between;
          }
        }
      }
    }
    .van-submit-bar{
      bottom: 49px;
    }

  }
}
</style>
